package com.itheima.collecton.set.treeset;

import java.util.TreeMap;
import java.util.TreeSet;

public class MyTreeSet {
    public static void main(String[] args) {
        TreeSet<Student> treeSet = new TreeSet<>();
        Student student1 = new Student("zhangsan", 21);
        Student student2 = new Student("lisi", 22);
        Student student3 = new Student("wangwu", 23);
        Student student4 = new Student("zhaoliu", 21);
        Student student5 = new Student("zhangsan", 21);

        treeSet.add(student1);
        treeSet.add(student2);
        treeSet.add(student3);
        treeSet.add(student4);
        treeSet.add(student5);

        for (Student stu : treeSet) {
            System.out.println(stu);
        }
    }
}

class Student implements Comparable<Student>{
    private String name;
    private int age;

    public Student() {
    }

    public Student(String name, int age) {
        this.name = name;
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }

    @Override
    public int compareTo(Student o) {
        //return this.name.hashCode() - o.name.hashCode();
        int result = this.age - o.age;  //主要判断条件
        //int String.compareTo(Stirng s)
        result = result == 0 ? this.name.compareTo(o.name) : result;    //次要判断条件
        //System.out.println("this.name.hashCode() - o.name.hashCode() = " + (this.name.hashCode() - o.name.hashCode()));
        //System.out.println("result = " + result);
        return result;
    }
}
